﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml;
using System.Xml.Linq;
using System.Threading;

namespace LastFmClient
{
    public class Client
    {
        public XElement GetXElement(string url)
        {

            if (TimerSingleton.Instance.XmlDocs.ContainsKey(url))
                return TimerSingleton.Instance.XmlDocs[url];
            
            //Thread.Sleep(1000); // to stop the client from accessing lastfm too often
            while (!TimerSingleton.Instance.IsAccessAllowed)
                Thread.Sleep(25);

            TimerSingleton.Instance.LastAccessed = DateTime.Now;

            XElement x = null;

            try
            {
                 x = XElement.Load(url);
            }
            catch (Exception e)
            {

            }

            TimerSingleton.Instance.XmlDocs.Add(url, x);

            return x;
        }


        //private static string m_TagUrl = "http://ws.audioscrobbler.com/1.0/artist/oasis/toptags.xml";

        //public Client()
        //{
        //    XElement x = XElement.Load(m_TagUrl);

        //    IEnumerable<Tag> tags =
        //        from el in x.Elements("tag")
        //        select new Tag(el);

        //    //string topTags = x.Elements()
        //    //    let price = (decimal)el.Element("Price")



        //    //IEnumerable<decimal> prices =
        //    //from el in x.Elements("Data")
        //    //let price = (decimal)el.Element("Price")
        //    //orderby price
        //    //select price;


        //}
    }
}
